window.onload = function(){

    /**
     * 加载音乐文件
     */
    function audiodeer(){

        var deerAudio = document.getElementById('deerAudio');
        var audio = document.createElement('audio');
        audio.setAttribute("autoplay", "loop");
        audio.setAttribute("id", "audio");

        var source = document.createElement('source');
        source.src = "/assets/audio/audio.mp3";
        source.type = "audio/mpeg";

        deerAudio.append(audio);
        audio.append(source);

    };

    audiodeer();

    /**
     * 每隔五分钟循环音乐
     */

    var sm = setInterval(audiodeer, 1000*60*5);

    /**
     * 控制音频暂停播放
     * @type {Element}
     */
    var mega = document.getElementById('icon-mega');

    mega.onclick = function(){

        var deerAudio = document.getElementById('audio');

        var megaName = mega.className;
        if( megaName == 'icon-volume-2' ){
            mega.className = "icon-volume-off";
            deerAudio.pause();
            clearInterval(sm);
        }
        else if( megaName == 'icon-volume-off' ){
            mega.className = "icon-volume-2";
            deerAudio.play();
        }
    }


    /**
     * loading
     */
    function loading(){
        var load = document.getElementById('loading');
        var loadbox = document.createElement('img');
        loadbox.src = "../images/loading.gif";

        load.append(loadbox);
    }

    loading();


    /**
     * 返回顶部
     * @type {Element}
     */

    var tpTop = document.getElementById('goTop');
    var time = null;
    var isTop = true;
        cancelScroll = false;

    var clientHeight = document.documentElement.clientHeight || document.body.clientHeight;


    window.onscroll = function(){
        var osTop = document.documentElement.scrollTop || document.body.scrollTop;
        console.log(osTop, clientHeight);

        if( osTop > clientHeight ){
            tpTop.style.display = 'block';
        }
        else{
            tpTop.style.display = 'none';
        }

        if( !isTop ){
            clearInterval(time);
        }
        isTop = false;
    }

    tpTop.onclick = function(){
        if( cancelScroll == false ){
            time = setInterval(function(){
                var osTop = document.documentElement.scrollTop || document.body.scrollTop;
                var isSpeed = Math.floor(-osTop/10);
                document.documentElement.scrollTop = document.body.scrollTop = osTop + isSpeed;
                isTop = true;

                if( osTop == 0 ){
                    clearInterval(time);
                    cancelScroll = true;
                }
            }, 30)
        }
        else{
            clearInterval(time);
            cancelScroll = false;
        }
    }




};

/**
 *
 * 生成随机背景
 * @returns {Function}
 */

function backImage(){
    var Image = [];
        Image[0] = 'http://img1.imgtn.bdimg.com/it/u=2880243692,128334846&fm=214&gp=0.jpg';
        Image[1] = 'http://img2.niutuku.com/desk/1207/1343/bizhi-1343-25218.jpg';
        Image[2] = 'http://img2.niutuku.com/desk/1207/1648/bizhi-1648-4098.jpg';
        Image[3] = 'http://img2.niutuku.com/desk/1207/1347/bizhi-1347-25749.jpg';
        Image[4] = 'http://tupian.enterdesk.com/2014/mxy/02/28/1/7.jpg';
        Image[5] = 'http://img2.niutuku.com/desk/1207/1332/bizhi-1332-23901.jpg';
        Image[6] = 'http://images1.tianxun.com/news/2015/09/01/wooden-museum-suzdal-russia.jpg';
        Image[7] = 'http://image95.360doc.com/DownloadImg/2016/02/2513/66631434_18.jpg';
        Image[8] = 'http://pic1.win4000.com/wallpaper/e/539ebd97cb335.jpg';
        Image[9] = 'http://image.tianjimedia.com/uploadImages/2011/321/1945K760205N.jpg';
        Image[10] = 'http://www.guandongphoto.com/data/attachment/forum/201604/23/063523yutf7vtzh9z2w042.jpg';
        Image[11] = 'http://img5.duitang.com/uploads/item/201603/13/20160313000730_fCyS4.jpeg';
        Image[12] = 'http://img3.duitang.com/uploads/item/201603/11/20160311102620_iSPGe.jpeg';
        Image[13] = 'http://pic.qiantucdn.com/58pic/18/31/15/57m58PIC5Qu_1024.jpg';
        Image[14] = 'http://img4.duitang.com/uploads/item/201409/24/20140924110422_kjFz5.png';
        Image[15] = 'http://img4.duitang.com/uploads/item/201405/25/20140525224953_QUYQQ.thumb.600_0.png';
        Image[16] = 'http://img1.gamedog.cn/2013/03/14/24-1303141149530.jpg';
        Image[17] = 'http://limg1.qunarzz.com/T1MXZTBXJT1R49Ip6K.jpeg_r_1920x1080x90_da600201.jpeg';
        Image[18] = 'http://pic.58pic.com/58pic/14/24/59/89W58PICaTV_1024.jpg';
        Image[19] = 'http://attach.bbs.china.com.cn/forum/201411/12/120443kheddolvsvezgqgg.jpg';
        Image[20] = 'http://cyjctrip.qiniudn.com/126959/1401423529243p18p5li333fov3s71m9e1ldr17tdp.jpg';

    var number = Math.floor(Math.random() * Image.length);

    return function(){
        return Image[number];
    }

}


backImage();




